package com.lqian.demo02.part4.service;

import com.lqian.demo02.part4.dao.BankDao;
import com.lqian.demo02.utils.CollectionManager;

import java.sql.SQLException;

/**
 * @author：Melody_LQ
 * @time：2022/7/14 12:59 下午
 */
public class BankService {

    private BankDao bankDao = new BankDao();


    /**
     * 转账
     * @param A 转款方
     * @param B 收款方
     * @param money 转款金额
     */
    public  void transfer(String A,String B,Float money){
        try {
            //开启事务
            CollectionManager.begin();
            int resultRows1 = bankDao.transfer(A, money);
            int i = 1/0;
            int resultRows2 = bankDao.receiver(B, money);
            if (resultRows1==1&&resultRows2==1){
                System.out.println("your trensfer accounts successfully!");
                CollectionManager.commit();
            }
        } catch (Exception e) {
            System.out.println("System inner [ERROR] ====> ROLLBACK...");
            //发生异常 事务回滚
            CollectionManager.rollback();
            System.out.println("ROLLBACK FINISHED");
        }finally {
            CollectionManager.close();
        }
    }
}
